package com.banciyuan.bcywebview.biz.detail.mixweb;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.DailyApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.biz.detail.EditActivity;
import com.banciyuan.bcywebview.biz.detail.atperson.AtPersonActivity;
import com.banciyuan.bcywebview.biz.detail.mixweb.bridge.EditorBridge;
import com.banciyuan.bcywebview.biz.detail.mixweb.bridge.ImageBridge;
import com.banciyuan.bcywebview.biz.post.uploadpic.IGetToken;
import com.banciyuan.bcywebview.biz.post.uploadpic.UploadPicHelper;
import com.banciyuan.bcywebview.biz.write.photoselecotor.model.PhotoModel;
import com.banciyuan.bcywebview.biz.write.photoselecotor.pselector.PhotoActivity;
import com.banciyuan.bcywebview.biz.write.write.WriteHelper;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Multi;

/**
 * Created by squall on 2016/11/25.
 */

public class MixWebEditorActivity extends BaseActivity implements View.OnClickListener {

    private static final String LOAD_PATH = "file:///android_asset/webview/resource/image_loading.gif";

    private WebView mWebView;
    private ImageView select_iv;
    private ImageView at_iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mixweb_editor);
        initUi();
        initWeb();
        initAction();
    }

    @Override
    protected void initUi() {
        mWebView = (WebView) findViewById(R.id.webcontainer);
        select_iv = (ImageView) findViewById(R.id.add_img);
        at_iv = (ImageView) findViewById(R.id.at_add_img);
    }


    @Override
    protected void initAction() {
        select_iv.setOnClickListener(this);
        at_iv.setOnClickListener(this);
    }

    private void initWeb() {
        ImageBridge imageBridge = new ImageBridge(this);
        EditorBridge editBridge = new EditorBridge(this);
        imageBridge.setOnLoadImageSucc(new ImageBridge.OnLoadImageSucc() {
            @Override
            public void onSucc(final String imageUri, final String localUri) {
                mWebView.post(new Runnable() {
                    @Override
                    public void run() {
                        mWebView.loadUrl("javascript:onImageLoadSuccess('" + imageUri + "','" + localUri + "')");
                    }
                });
            }

            @Override
            public void onFail(final String imageUri) {
                mWebView.post(new Runnable() {
                    @Override
                    public void run() {
                        mWebView.loadUrl("javascript:onImageLoadFailed('" + imageUri + "')");
                    }
                });
            }
        });

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//        mWebView.setWebContentsDebuggingEnabled(true);


        mWebView.addJavascriptInterface(imageBridge, "ImageBridge");
        mWebView.addJavascriptInterface(editBridge, "EditorBridge");
        mWebView.loadUrl("file:///android_asset/webview/html/editor.html");
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_img:
                startImagePic();
                break;
            case R.id.at_add_img:
                Intent intent = new Intent(this, AtPersonActivity.class);
                startActivityForResult(intent, EditActivity.GOATPERSON);
                break;
        }
    }

    private void startImagePic() {
        Intent intent = new Intent(this, PhotoActivity.class);
        startActivityForResult(intent, WriteHelper.PICK_IMAGE);
    }

    private void addImage(List<PhotoModel> templist) {
        for (int i = 0; i < templist.size(); i++) {
            final String id = "img" + i;
            mWebView.post(new Runnable() {
                @Override
                public void run() {
                    mWebView.loadUrl("javascript:insertImage('" + id + "','" + LOAD_PATH + "')");
                }
            });
        }
        updateImage(templist);
    }

    private void updateImage(List<PhotoModel> templist) {
        UploadPicHelper uploadPicHelper = new UploadPicHelper(this, new IGetToken() {
            @Override
            public void getTokenSucc(final List<Multi> path) {
                for (int i = 0; i < path.size(); i++) {
                    final String id = "img" + i;
                    final Multi m = path.get(i);
                    mWebView.post(new Runnable() {
                        @Override
                        public void run() {
                            mWebView.loadUrl("javascript:updateImage('" + id + "','" + m.getPath() + "','" + m.getW() + "','" + m.getH() + "')");
                        }
                    });
                }
            }

            @Override
            public void getTokenFail() {

            }
        }, true);

        uploadPicHelper.getToken("", "", DailyApi.getGetuploader(), templist);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == WriteHelper.PICK_IMAGE) {
            List<PhotoModel> templist = (ArrayList<PhotoModel>) data.getExtras().getSerializable("photos");
            addImage(templist);

        }
        if (requestCode == EditActivity.GOATPERSON && resultCode == RESULT_OK) {
            String personname = data.getStringExtra("personname");

        }
    }


}
